package top.lisicheng.service;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import top.lisicheng.pojo.po.Order;

import static top.lisicheng.common.constant.RabbitMqConstant.EMPTY_ROUTER_KEY;
import static top.lisicheng.common.constant.RabbitMqConstant.FANOUT_EXCHANGE;

/**
 * @author Lisicheng
 */
@Component
@Log4j2
@AllArgsConstructor
public class FanoutService {

    private final RabbitTemplate rabbitTemplate;

    public void order(Order order) {

        try {
            rabbitTemplate.convertAndSend(FANOUT_EXCHANGE, EMPTY_ROUTER_KEY, JSONObject.toJSONString(order));
        } catch (AmqpException e) {
            if (log.isDebugEnabled()) {
                log.debug("debug");
            }
            log.error("convert message error!", e);
        }

    }
}
